package me.techbird.nve.web.servlet;

import me.techbird.nve.domain.*;
import me.techbird.nve.service.UserService;
import me.techbird.nve.service.impl.UserServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {

    private UserService userService = new UserServiceImpl();

    /**
     * 查询所有用户信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String pageSizeStr = request.getParameter("pageSize");
        String currentPageStr = request.getParameter("currentPage");

        int currentPage = 1;//若不传递则默认第一页
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        int pageSize = 5;//若不传递则默认5条
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PageBean<User> pageBean = userService.pageQuery(currentPage, pageSize);
        writeValue(pageBean, response);
    }

    /**
     * 判断是否登录
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findOne(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        User user = null;
        HttpSession session = request.getSession(false);
        if (session != null) {
            user = (User) session.getAttribute("user");
        }
        System.out.println(user);
        writeValue(user, response);
    }

    /**
     * 登录
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        String username = request.getParameter("stu_no");
        String password = request.getParameter("password");

        System.out.println(username + "----" + password);
        ResultInfo info = new ResultInfo();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorMsg("请检查用户名或密码");
            e.printStackTrace();
        }

        User u = userService.login(user);
        System.out.println(u);

        if (u == null) {
            info.setFlag(false);
            info.setErrorMsg("请检查用户名或密码");
            request.getSession().invalidate();
        } else {
            info.setFlag(true);
            info.setErrorMsg("登录成功");
            info.setData(u);
            request.getSession().setAttribute("user", u);
        }
        System.out.println("---session1：" + request.getSession());
        writeValue(info, response);
    }

    /**
     * 退出
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //销毁session
        request.getSession().invalidate();
        //重定向到登录页面
        ResultInfo info = new ResultInfo();
        info.setFlag(true);
        writeValue(info, response);
    }

    /**
     * 根据uid查询用户信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findByUid(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid = request.getParameter("uid");
        if (uid != null) {
            User user = userService.findByUid(uid);
            writeValue(user, response);
        } else {
            writeValue(null, response);
        }
    }

    /**
     * 更新用户信息
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        ResultInfo info = new ResultInfo();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);

        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("user---" + user);
        boolean flag = userService.updateUser(user);
        info.setFlag(flag);
        writeValue(info, response);
    }

    /**
     * 注册新用户
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void regist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        ResultInfo info = new ResultInfo();
        User user = new User();
        try {
            BeanUtils.populate(user, parameterMap);
            user.setRole(2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        boolean flag = userService.addUser(user);
        info.setFlag(flag);
        writeValue(info, response);
    }

    /**
     * 根据uid删除
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uid = request.getParameter("uid");
        ResultInfo info = new ResultInfo();
        if (uid != null) {
            User user = new User();
            user.setUid(Integer.parseInt(uid));
            boolean flag = userService.removeUser(user);
            info.setFlag(flag);
        } else {
            info.setFlag(false);
        }
        writeValue(info, response);
    }

    /**
     * 提交实验报告
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void report(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> parameterMap = request.getParameterMap();
        System.out.println("----report:" + parameterMap);

        ResultInfo info = new ResultInfo();
        ExpReport report = new ExpReport();
        try {
            BeanUtils.populate(report, parameterMap);
            User user = (User) request.getSession().getAttribute("user");
            report.setUser(user);
        } catch (Exception e) {
            info.setFlag(false);
            info.setErrorMsg("提交失败");
            e.printStackTrace();
        }

        boolean flag = userService.addReport(report);
        info.setFlag(flag);
        if (flag) {
            info.setErrorMsg("提交成功");
            info.setData(report);
        } else {
            info.setErrorMsg("提交失败");
        }
        writeValue(info, response);
    }

    /**
     * 在线考试抽题
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void onlineQuestions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<ExamQuestion> questions = userService.findQuestions();
        System.out.println("---抽取试题：" + questions);
        writeValue(questions, response);
    }

    /**
     * 提交在线考试结果
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void onlineTest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String score = request.getParameter("score");
        ResultInfo info = new ResultInfo();
        if (null != score && !score.equals("")) {
            System.out.println("----提交测试：" + score);
            User user = (User) request.getSession().getAttribute("user");
            ExamTest test = new ExamTest();
            test.setScore(Integer.valueOf(score));
            test.setCourse("network");
            test.setUid(user.getUid());
            System.out.println(test);
            boolean flag = userService.addTestResult(test);
            info.setFlag(flag);
        } else {
            info.setFlag(false);
            info.setErrorMsg("请提交成绩");
        }
        writeValue(info, response);
    }

    /**
     * 查找所有试验报告
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findReports(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageSizeStr = request.getParameter("pageSize");
        String currentPageStr = request.getParameter("currentPage");

        int currentPage = 1;//若不传递则默认第一页
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        int pageSize = 5;//若不传递则默认5条
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PageBean<ExpReport> pageBean = userService.findAllReport(currentPage, pageSize);
        writeValue(pageBean, response);
    }

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    public void findTestResult(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String pageSizeStr = request.getParameter("pageSize");
        String currentPageStr = request.getParameter("currentPage");

        int currentPage = 1;//若不传递则默认第一页
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        int pageSize = 5;//若不传递则默认5条
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
        }

        PageBean<ExamTest> pageBean = userService.findAllTest(currentPage, pageSize);
        writeValue(pageBean, response);
    }

}
